package com.sanjiahantang.youjiaclient.module.city.models.impl;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.module.city.beans.CityBean;
import com.sanjiahantang.youjiaclient.module.city.models.ICityModel;
import com.sanjiahantang.uilibrary.utils.Cn2Spell;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2017/9/19.
 */

public class CityModel implements ICityModel {
    @Override
    public void loadCityList(final Context context, CallBack<CityBean> callBack) {
        try {
            InputStream is = context.getResources().getAssets().open("province_data.json");
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            Gson gson = new Gson();
            List<CityBean.Province> provinces = gson.fromJson(isr, new TypeToken<List<CityBean.Province>>() {
            }.getType());
            ArrayList<String> provinceList = new ArrayList<String>();
            ArrayList<CityBean.City> cityList = new ArrayList<CityBean.City>();
            if (provinces != null) {
                for (int i = 0, max = provinces.size(); i < max; i++) {
                    provinceList.add(provinces.get(i).getName());
                    if (null != provinces.get(i).getCity()) {
                        for (int j = 0, max_j = provinces.get(i).getCity().size(); j < max_j; j++) {
                            CityBean.City city = new CityBean.City();
                            city.setRegion_name(provinces.get(i).getCity().get(j).getName());
                            city.setPinyin(Cn2Spell.getPinYinFirstLetter(provinces.get(i).getCity().get(j).getName())) ;
                            cityList.add(city);
                        }
                    }
                }
            }
            CityBean bean = new CityBean();
            CityBean.Data data = new CityBean.Data();
            data.setHot_city(cityList);
            bean.setData(data);
            callBack.onSuccess(bean,context,null);
        } catch (IOException e) {
            // Should never happen!
            throw new RuntimeException(e);
        }
    }

    @Override
    public void getAllArea(final Context context, final BaseView.OnLoadListener callBack) {
        try {
            InputStream is = context.getResources().getAssets().open("province_data.json");
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            Gson gson = new Gson();
            List<CityBean.Province> provinces = gson.fromJson(isr, new TypeToken<List<CityBean.Province>>() {
            }.getType());
            ArrayList<String> provinceList = new ArrayList<String>();
            ArrayList<ArrayList<String>> cityList = new ArrayList<ArrayList<String>>();
            ArrayList<ArrayList<ArrayList<String>>> areaList = new ArrayList<ArrayList<ArrayList<String>>>();
            if (provinces != null) {
                for (int i = 0, max = provinces.size(); i < max; i++) {
                    provinceList.add(provinces.get(i).getName());
                    if (null != provinces.get(i).getCity()) {
                        ArrayList<String> cities = new ArrayList<String>();
                        ArrayList<ArrayList<String>> areas = new ArrayList<ArrayList<String>>();
                        for (int j = 0, max_j = provinces.get(i).getCity().size(); j < max_j; j++) {
                            cities.add(provinces.get(i).getCity().get(j).getName());
                            areas.add((ArrayList<String>) provinces.get(i).getCity().get(j).getArea());
                        }
                        cityList.add(cities);
                        areaList.add(areas);
                    }
                }
            }
            Object[] result = new Object[3];
            result[0] = provinceList;
            result[1] = cityList;
            result[2] = areaList;
            callBack.onLoad(result);
        } catch (IOException e) {
            // Should never happen!
            throw new RuntimeException(e);
        }
    }
}
